package gameFile;

import gameFile.GamePlay.*;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.*;
//import java.util.ArrayList;

import javax.swing.JOptionPane;

public class GraphicPage extends Applet implements Runnable, KeyListener, MouseListener{

	private static final long serialVersionUID = 1L; //this thing is damn annoying >:(
	final static int Width = 500;
	final static int Height = 400;
	private Thread thread;
	private Image backImage;
	private Graphics graphics;
	private GamePlay game;
	
//	WallSegment playerWall = new WallSegment();
//	WallSegment botWall[] = new WallSegment[3];
//	ArrayList<WallSegment> walls = new ArrayList<WallSegment>();
//	HumanPlayer playerBike = new HumanPlayer();
//	LightBike bots[] = new AIBot[3];
//	
	boolean leftKey = false;
	boolean rightKey = false;
	boolean upKey = false;
	boolean downKey = false;

	@Override
	public void init() {
		addKeyListener(this);
		addMouseListener(this);
		Frame mainFrame = (Frame)this.getParent().getParent();
		mainFrame.setTitle("Line Evasion");
	}

	@Override
	public void start() {
		thread = new Thread(this);
		game = new GamePlay(VSMode.VSCOMPUTER,Width,Height);
		backImage = createImage(Width,Height);
		graphics = backImage.getGraphics();
		this.setName("LineEvasion");
		this.resize(Width,Height);
		
		thread.start();
	}

	@Override
	public void stop() {
		thread = null;
	}

	public void run() {
		boolean movedLeft;
		boolean movedRight;
		while (!game.gameOver){
			
			movedLeft = false;
			movedRight = false;
			if (!movedLeft && leftKey){ 
				//walls.add(playerBike.getTrailingWall());
				game.setHumanAction(0, Action.GOLEFT); leftKey = false; movedLeft = true; 
			}
			if (!movedRight && rightKey){
				//walls.add(playerBike.getTrailingWall());
				game.setHumanAction(0, Action.GORIGHT); rightKey = false; movedRight = true;
			}
			game.playTurn();
			
			repaint();
			try {
				Thread.sleep(30);
			} catch (InterruptedException e){}
		}
			playAgain();
	}
	
	
		
	public void playAgain() {
		JOptionPane playAgainOption = new JOptionPane();
		int input = JOptionPane.showConfirmDialog(null, "Play Again?", "Play Again?", JOptionPane.YES_NO_OPTION);
		if (input == JOptionPane.YES_OPTION){
			start();
			repaint();
		} else {
			stop();
			System.exit(0);
		}
	}
	
	@Override
	public void update(Graphics g) {
		paint(graphics);
		g.drawImage(backImage, 0, 0, null);
	}

	@Override
	public void paint(Graphics g) {
		g.setColor(Color.BLACK);
		g.fillRect(0,0,Width, Height);
		
		game.drawGame(g);
	}

	@Override
	public void keyPressed(KeyEvent e){
		switch (e.getKeyCode()){
		case KeyEvent.VK_LEFT:
			leftKey = true;
			break;
		case KeyEvent.VK_RIGHT:
			rightKey = true;
			break;

		case KeyEvent.VK_UP:
			upKey = true;
			break;

		case KeyEvent.VK_DOWN:
			downKey = true;
			break;
		}
	}

	@Override
	public void keyReleased(KeyEvent eventKeyboard){
		int key = eventKeyboard.getKeyCode();
		switch (key){
		case KeyEvent.VK_LEFT:
			leftKey = false;
			break;
		case KeyEvent.VK_RIGHT:
			rightKey = false;
			break;
		case KeyEvent.VK_UP:
			upKey = false;
			break;
		case KeyEvent.VK_DOWN:
			downKey = false;
			break;
			// cheats
		case KeyEvent.VK_2:
			game.players[0].cheats(2);
			break;
		case KeyEvent.VK_1:
			game.players[0].cheats(1);
			break;
		case KeyEvent.VK_3:
			game.players[0].cheats(3);
			break;
		}
	}

	@Override
	public void keyTyped(KeyEvent e) {
		//unused
	}

	@Override
	public void mouseClicked(MouseEvent eventMouse) {
		game.players[0].moveBikeWithMouse(eventMouse);
	}

	@Override
	public void mouseEntered(MouseEvent eventMouse) {
		//unused
	}

	@Override
	public void mouseExited(MouseEvent eventMouse) {
		//unused
	}

	@Override
	public void mousePressed(MouseEvent eventMouse) {
		//unused
	}

	@Override
	public void mouseReleased(MouseEvent eventMouse) {
		//unused
	}
}

